# Call by value์™€ Call by reference


์ƒ๋‹นํžˆ ๊ธฐ๋ณธ์ ์ธ ์งˆ๋ฌธ์ด์ง€๋งŒ, ํ—ท๊ฐˆ๋ฆฌ๊ธฐ ์‰ฌ์šด ์ฃผ์ œ๋‹ค.


# call by value

๊ฐ’์— ์˜ํ•œ ํ˜ธ์ถœ

ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ, ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„ ์•ˆ์—์„œ๋Š” ํ•จ์ˆ˜๋ฅผ ์œ„ํ•œ ๋ณ„๋„์˜ ์ž„์‹œ๊ณต๊ฐ„์ด ์ƒ์„ฑ๋จ (์ข…๋ฃŒ ์‹œ ํ•ด๋‹น ๊ณต๊ฐ„ ์‚ฌ๋ผ์ง)

call by value ํ˜ธ์ถœ ๋ฐฉ์‹์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์ „๋‹ฌ๋˜๋Š” ๋ณ€์ˆ˜ ๊ฐ’์„ ๋ณต์‚ฌํ•ด์„œ ํ•จ์ˆ˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•จ

์ด๋•Œ ๋ณต์‚ฌ๋œ ์ธ์ž๋Š” ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ง€์—ญ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— local value ์†์„ฑ์„ ๊ฐ€์ง

๋”ฐ๋ผ์„œ, ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ธ์ž ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋”๋ผ๋„, ์™ธ๋ถ€ ๋ณ€์ˆ˜ ๊ฐ’์€ ๋ณ€๊ฒฝ์•ˆ๋จ

# ์˜ˆ์‹œ
void func(int n) {
    n = 20;
}

void main() {
    int n = 10;
    func(n);
    printf("%d", n);
}

printf๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฐ’์€ ๊ทธ๋Œ€๋กœ 10์ด ์ถœ๋ ฅ๋œ๋‹ค.


# call by reference

์ฐธ์กฐ์— ์˜ํ•œ ํ˜ธ์ถœ

call by reference ํ˜ธ์ถœ ๋ฐฉ์‹์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š” ๋ณ€์ˆ˜์˜ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ „๋‹ฌํ•จ

๋”ฐ๋ผ์„œ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ธ์ž ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด, ์•„๊ทœ๋จผํŠธ๋กœ ์ „๋‹ฌ๋œ ๊ฐ์ฒด์˜ ๊ฐ’๋„ ๋ณ€๊ฒฝ๋จ

void func(int *n) {
    *n = 20;
}

void main() {
    int n = 10;
    func(&n);
    printf("%d", n);
}

printf๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฐ’์€ 20์ด ๋œ๋‹ค.



# Java ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹

์ž๋ฐ”์˜ ๊ฒฝ์šฐ, ํ•จ์ˆ˜์— ์ „๋‹ฌ๋˜๋Š” ์ธ์ž์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋”ฐ๋ผ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹์ด ๋‹ฌ๋ผ์ง

  • primitive type(์›์‹œ ์ž๋ฃŒํ˜•) : call by value

    int, short, long, float, double, char, boolean

  • reference type(์ฐธ์กฐ ์ž๋ฃŒํ˜•) : call by reference

    array, Class instance


์ž๋ฐ”์˜ ๊ฒฝ์šฐ, ํ•ญ์ƒ call by value๋กœ ๊ฐ’์„ ๋„˜๊ธด๋‹ค.

C/C++์™€ ๊ฐ™์ด ๋ณ€์ˆ˜์˜ ์ฃผ์†Œ๊ฐ’ ์ž์ฒด๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋ฐฉ๋ฒ•์ด ์—†์œผ๋ฉฐ, ์ด๋ฅผ ๋„˜๊ธธ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ• ๋˜ํ•œ ์žˆ์ง€ ์•Š๋‹ค.

reference type(์ฐธ์กฐ ์ž๋ฃŒํ˜•)์„ ๋„˜๊ธธ ์‹œ์—๋Š” ํ•ด๋‹น ๊ฐ์ฒด์˜ ์ฃผ์†Œ๊ฐ’์„ ๋ณต์‚ฌํ•˜์—ฌ ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์‚ฌ์šฉํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ์›๋ณธ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๊นŒ์ง€๋Š” ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‚˜, ์›๋ณธ ๊ฐ์ฒด ์ž์ฒด๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜๋Š” ์—†๋‹ค.

์•„๋ž˜์˜ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ๋ด๋ณด์ž.



User a = new User("gyoogle");   // 1

foo(a);

public void foo(User b){        // 2
    b = new User("jongnan");    // 3
}

/*
==========================================

// 1 : a์— User ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ํ• ๋‹น(์ƒˆ๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด์˜ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์Œ)
 
 a   -----> User Object [name = "gyoogle"]
 
==========================================

// 2 : b๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์— a๊ฐ€ ๊ฐ€์ง„ ์ฃผ์†Œ๊ฐ’์„ ๋ณต์‚ฌํ•˜์—ฌ ๊ฐ€์ง

 a   -----> User Object [name = "gyoogle"]
               โ†‘     
 b   -----------
 
==========================================

// 3 : ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ์ฃผ์†Œ๊ฐ’์„ b๊ฐ€ ๊ฐ€์ง€๋ฉฐ a๋Š” ๊ทธ๋Œ€๋กœ ์›๋ณธ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ด
 
 a   -----> User Object [name = "gyoogle"]
                   
 b   -----> User Object [name = "jongnan"]
 
*/

ํŒŒ๋ผ๋ฏธํ„ฐ์— ๊ฐ์ฒด/๊ฐ’์˜ ์ฃผ์†Œ๊ฐ’์„ ๋ณต์‚ฌํ•˜์—ฌ ๋„˜๊ฒจ์ฃผ๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” Java๋Š” ์ฃผ์†Œ๊ฐ’์„ ๋„˜๊ฒจ ์ฃผ์†Œ๊ฐ’์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” call by reference๋ผ๊ณ  ์˜คํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋Š” C/C++์™€ Java์—์„œ ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹นํ•˜๋Š” ๋ฐฉ์‹์„ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋‹ค.



// c/c++ 
 
 int a = 10;
 int b = a;
 
 cout << &a << ", " << &b << endl; // out: 0x7ffeefbff49c, 0x7ffeefbff498
 
 a = 11;
 
 cout << &a << endl; // out: 0x7ffeefbff49c

//java
 
 int a = 10;
 int b = a;
 
 System.out.println(System.identityHashCode(a));    // out: 1627674070
 System.out.println(System.identityHashCode(b));    // out: 1627674070
 
 a = 11;

 System.out.println(System.identityHashCode(a));    // out: 1360875712

C/C++์—์„œ๋Š” ์ƒ์„ฑํ•œ ๋ณ€์ˆ˜๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ํ• ๋‹นํ•˜๊ณ  ์ด์— ๊ฐ’์„ ๋ฎ์–ด์”Œ์šฐ๋Š” ํ˜•์‹์œผ๋กœ ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค. (* ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ๊ฐ™์€ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.)

Java์—์„œ ๋˜ํ•œ ์ƒ์„ฑํ•œ ๋ณ€์ˆ˜๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ๊ฐ–๋Š” ๊ฒƒ์€ ๋งˆ์ฐฌ๊ฐ€์ง€์ง€๋งŒ, ๊ทธ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ๊ฐ’ ์ž์ฒด๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ฐ’์„ ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ํ• ๋‹นํ•˜๊ณ  ์ด ์ฃผ์†Œ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ด๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.


  C/C++        |        Java
               |
a -> [ 10 ]    |   a -> [ XXXX ]     [ 10 ] -> XXXX(์œ„์น˜)
b -> [ 10 ]    |   b -> [ XXXX ]
               |
             ๊ฐ’ ๋ณ€๊ฒฝ
a -> [ 11 ]    |   a -> [ YYYY ]     [ 10 ] -> XXXX(์œ„์น˜)
b -> [ 10 ]    |   b -> [ XXXX ]     [ 11 ] -> YYYY(์œ„์น˜)

b = a;์ผ ๋•Œ a์˜ ๊ฐ’์„ b์˜ ๊ฐ’์œผ๋กœ ๋ฎ์–ด ์”Œ์šฐ๋Š” ๊ฒƒ์€ ๊ฐ™์ง€๋งŒ, ์‹ค์ œ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ’์˜ ์ฃผ์†Œ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ์˜ ์ฐจ์ด๊ฐ€ ์กด์žฌํ•œ๋‹ค.

์ฆ‰, Java์—์„œ์˜ ๋ณ€์ˆ˜๋Š” [ํ• ๋‹น๋œ ๊ฐ’์˜ ์œ„์น˜]๋ฅผ [๊ฐ’]์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

C/C++์—์„œ๋Š” ์ฃผ์†Œ๊ฐ’ ์ž์ฒด๋ฅผ ์ธ์ž๋กœ ๋„˜๊ฒผ์„ ๋•Œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ ๋ฎ์–ด ์“ฐ์—ฌ ๊ธฐ์กด ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๊ณ , Java์—์„œ๋Š” ์ฃผ์†Œ๊ฐ’์ด ๋ฎ์–ด ์“ฐ์—ฌ์ง€๋ฏ€๋กœ ์›๋ณธ ๊ฐ’์€ ์ „ํ˜€ ์˜ํ–ฅ์ด ๊ฐ€์ง€ ์•Š๋Š” ๊ฒƒ์ด๋‹ค. (๊ฐ์ฒด์˜ ์†์„ฑ๊ฐ’์— ์ ‘๊ทผํ•˜์—ฌ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์€ ์ง์ ‘ ์ ‘๊ทผํ•˜์—ฌ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์ด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ณ€์ˆ˜๋“ค์—์„œ ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚œ๋‹ค.)



๊ฐ์ฒด ์ ‘๊ทผํ•˜์—ฌ ์†์„ฑ๊ฐ’ ๋ณ€๊ฒฝ

a : [ XXXX ]  [ Object [prop : ~ ] ] -> XXXX(์œ„์น˜)
b : [ XXXX ]

prop : ~ (์ด ๋˜ํ•œ ๋ณ€์ˆ˜์ด๋ฏ€๋กœ ์–ด๋”˜๊ฐ€์— ~๊ฐ€ ์ €์žฅ๋˜์–ด์žˆ๊ณ  prop๋Š” ์ด์˜ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์…ˆ)
prop : [ YYYY ]    [ ~ ] -> YYYY(์œ„์น˜)

a.prop = * (a๋ฅผ ํ†ตํ•ด prop๋ฅผ ๋ณ€๊ฒฝ) 

prop : [ ZZZZ ]    [ ~ ] -> YYYY(์œ„์น˜)
                   [ * ] -> ZZZZ

b -> Object์— ์ ‘๊ทผ -> prop ์ ‘๊ทผ -> ZZZZ

์œ„์™€ ๊ฐ™์€ ์ด์œ ๋กœ Java์—์„œ ์ธ์ž๋กœ ๋„˜๊ธธ ๋•Œ๋Š” ์ฃผ์†Œ๊ฐ’์ด๋ž€ ๊ฐ’์„ ๋ณต์‚ฌํ•˜์—ฌ ๋„˜๊ธฐ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ call by value๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ถœ์ฒ˜ : Is Java โ€œpass-by-referenceโ€ or โ€œpass-by-valueโ€? - Stack Overflow (opens new window)


# ์ •๋ฆฌ

Call by value์˜ ๊ฒฝ์šฐ, ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๋ณต์‚ฌํ•ด์„œ ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์›๋ณธ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์—†๋‹ค. ํ•˜์ง€๋งŒ ์ธ์ž๋ฅผ ๋„˜๊ฒจ์ค„ ๋•Œ๋งˆ๋‹ค ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ํ• ๋‹นํ•ด์•ผํ•ด์„œ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ๋” ์žก์•„๋จน๋Š”๋‹ค.

Call by reference์˜ ๊ฒฝ์šฐ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„ ํ• ๋‹น ๋ฌธ์ œ๋Š” ํ•ด๊ฒฐํ–ˆ์ง€๋งŒ, ์›๋ณธ ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ์œ„ํ—˜์ด ์กด์žฌํ•œ๋‹ค.

์ตœ์ข… ์ˆ˜์ • : 12/17/2022, 7:23:59 AM